package com.whq.thrift.api.web.admin.controller.bs;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.whq.common.StringUtil;
import com.whq.thrift.api.web.admin.db.baseconf.ModuleInfo;
import com.whq.thrift.api.web.admin.service.baseconf.ModuleInfoService;
import com.whq.thrift.api.web.admin.util.PageData;

@Controller
@Scope("prototype")
public class IceServiceController {
	private static final int DEFAULT_PAGE_SIZE = 25;

	@Autowired
	private ModuleInfoService moduleInfoService;

	@RequestMapping(value="bs/iceservice/list")
	public String list(
	        @RequestParam(value="moduleId", required=false) String moduleId,
	        @RequestParam(value="moduleStatus", required=false) Integer moduleStatus,
	        @RequestParam(value="currentPage", required=true, defaultValue="1") Integer currentPage,
	        @RequestParam(value="pageSize", required=false) Integer pageSize,
			Model model){  
	    PageData<ModuleInfo> pageData = new PageData<ModuleInfo>();
	    pageData.setCurrentPage(currentPage);
	    if ( (pageSize == null) || (pageSize <= 0) ){
	    	pageSize = DEFAULT_PAGE_SIZE;
	    }
	    pageData.setPageSize(pageSize);
	    
	    
		// 查询参数
	    Map<String, Object> whereParams = new HashMap<String, Object>(0);
	    if (StringUtil.isNotBlank(moduleId)) {
	    	whereParams.put("moduleId", moduleId);
	    }
	    
	    if(moduleStatus!=null){
	    	whereParams.put("moduleStatus", moduleStatus);
	    }
	    
	    int totalCount = moduleInfoService.findRecordCount(whereParams);
	    pageData.setTotalCount(totalCount);
	    
	    int beginIndex = (currentPage - 1) * pageSize;
	    List<ModuleInfo> resultList = moduleInfoService.findRecordsByMap(whereParams, beginIndex, pageSize);
	    pageData.setResultList(resultList);
	    
	    model.addAttribute("moduleId", moduleId);
	    model.addAttribute("moduleStatus",moduleStatus);
		model.addAttribute("pageData", pageData);
		return "bs/iceservice/list";
	}
	
	@RequestMapping(value="bs/iceservice/show")
	public String show(@RequestParam(value="id", required=true) String id, 
			Model model){ 
		if (id == null) {

			return "error";
		}
		ModuleInfo record = this.moduleInfoService.findRecordById(id);
        model.addAttribute("record", record);
        if (record == null) {

			return "error";
		}
        return "bs/iceservice/show";  
    }
	
	@RequestMapping(value="bs/iceservice/showAdd")
	public String showAdd(Model model){
		ModuleInfo record = new ModuleInfo();
		model.addAttribute("record", record);
		return "bs/iceservice/showAdd";
	}
	
	@RequestMapping(value="bs/iceservice/add")
	public String add(@Valid ModuleInfo record, 
			BindingResult result, 
			Model model) {
		ModuleInfo existRecord = this.moduleInfoService.findRecordById(record.getModuleId());
		if (existRecord != null) {

			model.addAttribute("record", record);
			return "bs/iceservice/showAdd";
		}
		
		this.moduleInfoService.addRecord(record);
		return "redirect:show.do?id=" + record.getModuleId();
	}
	
	@RequestMapping(value="bs/iceservice/showUpd")
	public String showUpd(@RequestParam(value="id", required=true) String id, 
			Model model){
		if (id == null) {

			return "error";
		}
		ModuleInfo record = this.moduleInfoService.findRecordById(id);
		if (record == null) {
			return "error";
		}
		model.addAttribute("record", record);
		return "bs/iceservice/showUpd";
	}
	
	@RequestMapping(value="bs/iceservice/upd")
	public String upd(@Valid ModuleInfo record, 
			BindingResult result, Model model) {
		if (result.hasErrors()) {
			model.addAttribute("record", record);
			return "bs/iceservice/showUpd";
		}
		
		this.moduleInfoService.updateRecord(record);
		return "redirect:show.do?id=" + record.getModuleId();
	}
	
	@RequestMapping(value="bs/iceservice/del")
    public String del(@RequestParam(value = "id", required = true, defaultValue = "0") String id,
            Model model) {
        this.moduleInfoService.deleteRecord(id);
        return "bs/iceservice/delOk";
    }
    
    @RequestMapping(value="bs/iceservice/delRecords")
    public String delRecords(String[] delIds,
            Model model) {
        if (delIds != null) {
            for (int i = 0; i < delIds.length; i++) {
                if (delIds[i] != null) {
                    this.moduleInfoService.deleteRecord(delIds[i]);
                }
            }
        }
        return "redirect:list.do";
    }
	
}